from rest_framework import serializers
from user import models


class RoleSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Role
        fields = '__all__'


class ResourceSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Resource
        fields = '__all__'


class Role_ResourceSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Role_Resource
        fields = '__all__'


class UserSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()
    avatar = serializers.SerializerMethodField()
    
    class Meta:
        model = models.User
        fields = ['id', 'username', 'phone', 'email', 'name', 'avatar']
    
    def get_name(self, obj):
        # 优先返回用户名，如果没有则返回手机号
        return obj.username or obj.phone
    
    def get_avatar(self, obj):
        # 这里可以根据需要返回用户头像URL
        # 暂时返回空字符串，后续可以扩展
        return ""


class UserLoginSerializer(serializers.Serializer):
    username = serializers.CharField(max_length=100)
    password = serializers.CharField(max_length=100)


class UserProfileSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()
    avatar = serializers.SerializerMethodField()
    
    class Meta:
        model = models.User
        fields = ['id', 'username', 'email', 'name', 'avatar']
    
    def get_name(self, obj):
        return obj.username or obj.phone
    
    def get_avatar(self, obj):
        return ""


class JobInfoSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.JobInfo
        fields = '__all__'


class ThirdLoginSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.ThirdLogin
        fields = '__all__'


class CitySerializer(serializers.ModelSerializer):
    class Meta:
        model = models.City
        fields = '__all__'


class HRInfoSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.HRInfo
        fields = '__all__'


class CompanySerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Company
        fields = '__all__'
